<?php
/*
 *	aip结果输出模块
 */
namespace app\api\controller;

use think\facade\View;

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

class Result extends Base{

    public function index(){
    	
    }
	
	/*
	 *	输出二维码图片
	 */
	public function rqcode(){
		$str=I('get.str');
		$pass=I('get.pass',0);
		$size=I('get.size',300);
		$down=I('get.d',0,'intval');			//保存本地
		
		switch ($pass) {
			case '64':
				$str=base64_decode($str);
				break;
			default:
				break;
		}
		
		$writer = new PngWriter();

		$qrCode = QrCode::create($str)
		->setEncoding(new Encoding('UTF-8'))
		->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
		->setSize($size)
		->setMargin(10)
		->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
		->setForegroundColor(new Color(0, 0, 0))
		->setBackgroundColor(new Color(255, 255, 255));
		
		$result = $writer->write($qrCode);
		
		if($down==1){
			$sha1 = sha1($str);
			$qrcode_dir = env('path.qrcode') . '/' . substr($sha1, 0, 3)  .'/'.substr($sha1,6,3) . '/';
			if (!file_exists($qrcode_dir)) mkdir($qrcode_dir, 0777, true);
			$file_name = $qrcode_dir .$sha1 . '.png';
			if (is_file($file_name)) {
				//echo $file_name;
				echo file_get_contents($file_name);
				die;
			}else{
				$result->saveToFile($file_name);
			}
		}
		
		header('Content-Type: '.$result->getMimeType());
		
		echo $result->getString();
		die();
	}
}
